package com.westos.haohanbang.servlet;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        HttpSession session = request.getSession();
        String str = (String) session.getAttribute("ranstr");
        if (StringUtils.equalsIgnoreCase(request.getParameter("username"), "周雄") && StringUtils.equalsIgnoreCase(request.getParameter("password"), "123456") && StringUtils.equalsIgnoreCase(request.getParameter("yanzhengma"), str)) {

            response.sendRedirect(request.getContextPath()+"/haohanbang.jsp");

        } else if (!StringUtils.equalsIgnoreCase(request.getParameter("username"), "周雄") || !StringUtils.equalsIgnoreCase(request.getParameter("password"), "123456")) {

            response.getWriter().println("用户名或密码错误");
            response.getWriter().println("请重新 <a href=\"/HaohanVueAjax/login.html\" >登录</a>");

        }
        else if (!StringUtils.equalsIgnoreCase(request.getParameter("yanzhengma"), str)) {

            response.getWriter().println("验证码错误");
            response.getWriter().println("请重新 <a href=\"/HaohanVueAjax/login.html\" >登录</a>");
        }

    }

}

